package com.hall.controller;

import com.hall.pojo.CommentData;
import com.hall.pojo.DoctorAppraise;
import com.hall.pojo.SysUser;
import com.hall.service.*;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/appraise")
public class DoctorAppraiseContorller {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private HallServiceImpl hallService;
    @Autowired
    private DoctorAppraiseServiceImpl doctorAppraiseService;
    @ModelAttribute
    public DoctorAppraise get(@RequestParam(required=false) String id) {
        DoctorAppraise entity = null;
        if (!StringUtils.isNullOrEmpty(id)){
            entity = doctorAppraiseService.findAppriaseId(id);
        }
        if (entity == null){
            entity =new DoctorAppraise();
        }
        return entity;
    }
    /*需要参数 当前登录用户sysUserId,会议appointmentId*/
   @RequestMapping("view")
    public String viewAppraise(HttpServletRequest request,String sysUserId, String appointmentId, Model model) {
       HttpSession sessionB = request.getSession();
       ServletContext ContextB = sessionB.getServletContext();
       HttpSession session=(HttpSession)ContextB.getContext("/boke").getAttribute("session");
       if(session!=null){
           sysUserId = (String) session.getAttribute("userId");
       }
       List<SysUser> sysUserList=hallService.findAppointUserList(appointmentId);
       //界面显示各专家目前的平均分
       String ratynumber="[";
       for(SysUser user:sysUserList){
               ratynumber+="{\"number\":\""+(StringUtils.isNullOrEmpty(user.getAppointmentAvgScore())?0:user.getAppointmentAvgScore())+"\"},";}
       ratynumber=ratynumber.substring(0, ratynumber.length()-1);
       ratynumber=ratynumber+"]";
       model.addAttribute("appointmentId",appointmentId);//当前会议
       model.addAttribute("sysUserId",sysUserId);//当前登录用户
       model.addAttribute("sysUserlist",sysUserList);
       model.addAttribute("ratynumber",ratynumber);
       return "doctorlist";
   }
   @RequestMapping("save")
   @ResponseBody
    public String saveAppraise(DoctorAppraise doctorAppraise){
       doctorAppraiseService.saveAppraise(doctorAppraise);
       return "OK";
   }
}
